import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class GameObjectPanel extends JComponent implements IGameObjectPanel {

	private Option gameOption;
	private Menu parent;
	private int score1;
	private int score2;
	
	public GameObjectPanel(Option opzioni) {
		gameOption = opzioni;
		parent = opzioni.getMenu();
		score1 = 0;
		score2 = 0;
		gameOption.setMaxScore(gameOption.MAX_SCORE); // max score = 5
		// Setting Balls
		gameOption.setNormalBall(new Ball(gameOption));
		if (gameOption.getnBalls() == 2) {
			gameOption.setOtherBall(new Ball(gameOption));
			// Setting Other Ball Starting Position
			gameOption.getOtherBall().setPos(100, 100);
		}
		// Setting Ball starting position
		gameOption.getNormalBall().setPos(147, 186);

		gameOption.setP1(new Player(gameOption, 1));
		if (gameOption.isCom()) {
			gameOption.setP2(new Com(gameOption, 0));
			gameOption.setComP((Com) gameOption.getP2());
		} else {
			gameOption.setP2(new Player(gameOption, 2));
		}
		this.setFocusable(true);
		this.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				switch (e.getKeyCode()) {
				case KeyEvent.VK_W:
					gameOption.getP1().setMoveUp(true);
					break;
				case KeyEvent.VK_S:
					gameOption.getP1().setMoveDown(true);
					break;
				}
				
				if (!gameOption.isCom())
					switch (e.getKeyCode()) {
					case KeyEvent.VK_UP:
						gameOption.getP2().setMoveUp(true);
						break;
					case KeyEvent.VK_DOWN:
						gameOption.getP2().setMoveDown(true);
						break;
					}

				if (e.getKeyCode() == KeyEvent.VK_P) {
					if (gameOption.isNotInPause()) {
						gameOption.setIsNotInPause(false);
						repaint();		// fixa il problema della scritta "PAUSA" a volte non visibile quando si preme P
						System.out.println("PAUSA");
					} else if (gameOption.isNotInPause() == false) {
						gameOption.setIsNotInPause(true);
						synchronized (gameOption) {
							gameOption.notifyAll();
						}
						System.out.println("pausa tolta.");
					}

				}

			}

			public void keyReleased(KeyEvent e) {
				switch(e.getKeyCode()) {
				case KeyEvent.VK_W:
					gameOption.getP1().setMoveUp(false);
					break;
				case KeyEvent.VK_S:
					gameOption.getP1().setMoveDown(false);
					break;
				}

				if (!gameOption.isCom())
					switch (e.getKeyCode()) {
					case KeyEvent.VK_UP:
						gameOption.getP2().setMoveUp(false);
						break;
					case KeyEvent.VK_DOWN:
						gameOption.getP2().setMoveDown(false);
						break;
					}
			}
		});

	}

	public JFrame getParentFrame() {
		return parent;
	}

	public void setParentFrame(Menu x) {
		parent = x;

	}

	public void updateScoreP1() {
		if (score1 == gameOption.getMaxScore() - 1) {
			System.out.println("GIOCATORE 1 VINCE ");
			// FAI PARTIRE L OPTION
			RivincitaFrame testOption = new RivincitaFrame(gameOption.getResource().getString("player"),
					gameOption);

			gameOption.getMainFrameGame().setVisible(false);
			testOption.setVisible(true);
			gameOption.setRunning(false);

		} else {
			if (gameOption.getnBalls() == 2) {
				if (gameOption.getOtherBall().getPosX() + Ball.BALL_HEIGTH == gameOption
						.getMenu().getWidth()) {
					gameOption.getOtherBall().setPos(
							gameOption.getMenu().getSize().width / 2, 180);
					gameOption.getOtherBall().setXDirection(-1);
				} else {
					gameOption.getNormalBall().setPos(
							gameOption.getMenu().getSize().width / 2, 180);
					gameOption.getNormalBall().setXDirection(-1);
				}

			} else {
				gameOption.getNormalBall().setPos(
						gameOption.getMenu().getSize().width / 2, 180);
				gameOption.getNormalBall().setXDirection(-1);
			}
			score1++;
			System.out.println("GIOCATORE 1 : " + score1);
		}

	}

	public void updateScoreP2() {
		if (score2 == gameOption.getMaxScore() - 1) {
			String x;
			if (gameOption.getP2().getNumberPlayer() == 0)
				x = "COM ";
			else
				x = gameOption.getResource().getString("player");
			System.out.println(x +  " "+ gameOption.getResource().getString("win"));
			// FAI PARTIRE L OPTION
			RivincitaFrame testOption = new RivincitaFrame(x, gameOption);
			gameOption.getMainFrameGame().setVisible(false);

			testOption.setVisible(true);
			gameOption.setRunning(false);

		} else {
			if (gameOption.getnBalls() == 2) {
				if (gameOption.getOtherBall().getPosX() == 0) {
					gameOption.getOtherBall().setPos(
							gameOption.getMenu().getSize().width / 2, 180);
					gameOption.getOtherBall().setXDirection(1);
				} else {
					gameOption.getNormalBall().setPos(
							gameOption.getMenu().getSize().width / 2, 180);
					gameOption.getNormalBall().setXDirection(1);
				}

			} else {
				gameOption.getNormalBall().setPos(
						gameOption.getMenu().getSize().width / 2, 180);
				gameOption.getNormalBall().setXDirection(1);
			}
			score2++;
			System.out.println("GIOCATORE 2 : " + score1);
		}
	}

	public void paintComponent(Graphics g) {

		super.paintComponent(g);

		Graphics2D g2d = (Graphics2D) g;
   
		if (gameOption.isNotInPause()) {

			// PALLA // PONG
			g2d.drawImage(gameOption.getNormalBall().getImage(), gameOption
					.getNormalBall().getPosX(), gameOption.getNormalBall()
					.getPosY(), gameOption.getNormalBall().BALL_WIDTH,
					gameOption.getNormalBall().BALL_HEIGTH, null);

			if (gameOption.getnBalls() == 2) {
				g2d.drawImage(gameOption.getOtherBall().getImage(), gameOption
						.getOtherBall().getPosX(), gameOption.getOtherBall()
						.getPosY(), gameOption.getOtherBall().BALL_WIDTH,
						gameOption.getOtherBall().BALL_HEIGTH, null);
			}

			if (gameOption.isCom()) {

				Com comPlayer = gameOption.getCom();
				int puntoMedioPaddleY = (Player.RECT_HEIGHT / 2)
						+ comPlayer.getPosY();
				int paddleX = comPlayer.getPosX();
				int comSpeed = 0;

				switch (gameOption.getDifficolta()) {
				default:
					comSpeed = gameOption.LOW_SPEED;
					break;
				case 2:
					comSpeed = gameOption.HIGH_SPEED;
					break;
				case 3:
					comSpeed = gameOption.MEDIUM_SPEED;
					break;
				case 4:
					comSpeed = gameOption.LOW_SPEED;
					break;
				} // Fine switch

				// calcola la distanza tra il punto medio del paddle e l x,y
				// della palla

				int distanza = (int) Math.sqrt((Math.abs(paddleX
						- gameOption.getNormalBall().getPosX()) + Math
						.abs(puntoMedioPaddleY
								- gameOption.getNormalBall().getPosY())));

				if (distanza < 25) {

					if (comPlayer.getPosY() > gameOption.getNormalBall()
							.getPosY()) {

						comPlayer.setYDir(-comSpeed);

					} else {
						// SCENDI
						comPlayer.setYDir(+comSpeed);

					}
				}
				

				if (gameOption.getnBalls() == 2) {
					int distanzaBall2 = (int) Math.sqrt((Math.abs(paddleX
							- gameOption.getOtherBall().getPosX()) + Math
							.abs(puntoMedioPaddleY
									- gameOption.getOtherBall().getPosY())));

					if (distanzaBall2 < 20) {
						System.out.println("DISTANZA TRA PADDLE COM E BALL2 : "
								+ distanzaBall2);

						if (gameOption.getOtherBall().getPosY() < comPlayer
								.getPosY()) {

							// SALI
							comPlayer.setYDir(-1);

//						} else {
							// SCENDI
							comPlayer.setYDir(+1);

						}
					} // FINE IF DISTANZABALL < 25

				} // FINE IF DELLA SECONDA PALLA

				// DRAW THE SECOND PADDLE COM !!!
				g2d.drawImage(comPlayer.getImage(), comPlayer.getPosX(),
						comPlayer.getPosY(), Player.RECT_WIDTH,
						Player.RECT_HEIGHT, null);

			} else {
				// SE IL GIOCO NON E' VS COM , DISEGNA IL PADDLE DEL P2
				g2d.drawImage(gameOption.getP2().getImage(), gameOption.getP2()
						.getPosX(), gameOption.getP2().getPosY(),
						Player.RECT_WIDTH, Player.RECT_HEIGHT, null);

			}

			// PRIMO PONG A SINISTRA
			g2d.drawImage(gameOption.getP1().getImage(), gameOption.getP1()
					.getPosX(), gameOption.getP1().getPosY(),
					Player.RECT_WIDTH, Player.RECT_HEIGHT, null);

			// PUNTEGGI
			g2d.setColor(new Color(0, 191, 255));
			g2d.setFont(new Font("Garamond", 1, 30));
			g2d.drawString("" + score1,
					((parent.getContentPane().getWidth()) / 2) - 50, 40);
			g2d.setColor(new Color(0, 191, 255));
			g2d.drawString("" + score2,
					((parent.getContentPane().getWidth()) / 2) + 35, 40);
		} else {

			g2d.setColor(new Color(0, 191, 255));
			g2d.setFont(new Font("Garamond", 1, 50));
			g2d.drawString(gameOption.getResource().getString("pause"),
					((parent.getContentPane().getWidth()) / 2) - 80, 250);
		}
		
		

	}

	public int getScoreP1() {

		return score1;
	}

	public int getScoreP2() {

		return score2;
	}

	public boolean ballCollision() {
		// DA IMPLEMENTARE
		return false;
	}

}
